<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/blog/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    hostname: new URL('https://guodh.gitee.io/blog').hostname,
    root: '/blog/',
    scheme: 'Pisces',
    version: '7.7.1',
    exturl: false,
    sidebar: {"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},
    copycode: {"enable":true,"show_result":true,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    comments: {"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}
  };
</script>

  <meta name="description" content="好久没写文章了，最近都是一直996，难得有机会空下来写点东西。事情的缘由是在一个开发群里面有人问了个问题:  他是做网吧系统的，如果给每个网吧一个队列，上千个队列，RocketMQ是否能够支持？  虽然平时自诩对MQ的理解还是可以的，但是这么一个问题好像我对MQ的理解仅仅是名词上的理解，所以赶紧打开ide重新看下MQ的源码。 整体结构  先从整体上理解MQ的组成，主要有Producer、Names">
<meta property="og:type" content="article">
<meta property="og:title" content="MQ的Producer理解">
<meta property="og:url" content="https://guodh.gitee.io/blog/2017/08/13/2017/MQ%E7%9A%84Producer%E7%90%86%E8%A7%A3/index.html">
<meta property="og:site_name" content="蛋蛋的小屋">
<meta property="og:description" content="好久没写文章了，最近都是一直996，难得有机会空下来写点东西。事情的缘由是在一个开发群里面有人问了个问题:  他是做网吧系统的，如果给每个网吧一个队列，上千个队列，RocketMQ是否能够支持？  虽然平时自诩对MQ的理解还是可以的，但是这么一个问题好像我对MQ的理解仅仅是名词上的理解，所以赶紧打开ide重新看下MQ的源码。 整体结构  先从整体上理解MQ的组成，主要有Producer、Names">
<meta property="og:locale">
<meta property="og:image" content="https://olwr1lamu.qnssl.com/%E6%95%B4%E4%BD%93.png">
<meta property="og:image" content="https://olwr1lamu.qnssl.com/producer.png">
<meta property="article:published_time" content="2017-08-13T06:33:12.000Z">
<meta property="article:modified_time" content="2017-08-13T08:27:13.000Z">
<meta property="article:author" content="RunningEgg">
<meta property="article:tag" content="技术">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://olwr1lamu.qnssl.com/%E6%95%B4%E4%BD%93.png">

<link rel="canonical" href="https://guodh.gitee.io/blog/2017/08/13/2017/MQ%E7%9A%84Producer%E7%90%86%E8%A7%A3/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>MQ的Producer理解 | 蛋蛋的小屋</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">蛋蛋的小屋</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">来了就看看吧，没有刀剑只有故事</p>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-fw fa-user"></i>About</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>

  </li>
        <li class="menu-item menu-item-book">

    <a href="/blog/book/" rel="section"><i class="fa fa-fw fa-archive"></i>book</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://guodh.gitee.io/blog/2017/08/13/2017/MQ%E7%9A%84Producer%E7%90%86%E8%A7%A3/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="RunningEgg">
      <meta itemprop="description" content="写下我的点点滴滴，写下我对这个世界的看法，还有我内心锁崇拜的技术">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="蛋蛋的小屋">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          MQ的Producer理解
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2017-08-13 14:33:12 / Modified: 16:27:13" itemprop="dateCreated datePublished" datetime="2017-08-13T14:33:12+08:00">2017-08-13</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>好久没写文章了，最近都是一直996，难得有机会空下来写点东西。事情的缘由是在一个开发群里面有人问了个问题:</p>
<blockquote>
<p>他是做网吧系统的，如果给每个网吧一个队列，上千个队列，RocketMQ是否能够支持？</p>
</blockquote>
<p>虽然平时自诩对MQ的理解还是可以的，但是这么一个问题好像我对MQ的理解仅仅是名词上的理解，所以赶紧打开ide重新看下MQ的源码。</p>
<h2 id="整体结构"><a href="#整体结构" class="headerlink" title="整体结构"></a>整体结构</h2><img src="https://olwr1lamu.qnssl.com/%E6%95%B4%E4%BD%93.png" alt="整体图"/>

<p>先从整体上理解MQ的组成，主要有Producer、Namesrv、Broker、Consumer组成。而Producer、Broker、Consumer之间的通信主要依靠Netty实现。整体的架构看似简单，但是真的去阅读源码，才发现MQ在可靠性以及扩展性上提供了很好的支持，这才是它作为一款生产级产品最大的特色。<br>可靠性体现在MQ的至少一次投递，以及能够追溯消息。而这次我们可以通过Producer来粗窥MQ的扩展性。</p>
<h2 id="Procuder如何发送消息"><a href="#Procuder如何发送消息" class="headerlink" title="Procuder如何发送消息"></a>Procuder如何发送消息</h2><img src="https://olwr1lamu.qnssl.com/producer.png" alt="Procuder发送过程"/>

<p>Procuder即消息的生产者，NameSrv中取获得当前Topic的相关信息。这里没有画出来的是，Procuder在内部缓存了一份TopicTable，只有当缓存失效时才去NameSrv获取。<br>获取到的TopicInfo中包含了一个List<Queue>, 这个东西很值得思考。在代码中可以看到，每一次消息投递，都对List<Queue>做了一个取余的处理。</p>
<p>那么就提出了一下几个问题：</p>
<hr>
<ol>
<li>这个Queue是什么东西？</li>
<li>这个Queue有什么用？</li>
<li>为何要维护多个Queue呢？搞一个不就行了吗？</li>
</ol>
<hr>
<p>打开类一看，长这个鸟样。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MessageQueue</span> <span class="keyword">implements</span> <span class="title">Comparable</span>&lt;<span class="title">MessageQueue</span>&gt;, <span class="title">Serializable</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String topic;</span><br><span class="line">    <span class="keyword">private</span> String brokerName;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> queueId;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>查阅了相关资料，来自<a href="https://jaskey.github.io/blog/2016/12/19/rocketmq-rebalance%E3%80%82">https://jaskey.github.io/blog/2016/12/19/rocketmq-rebalance。</a><br>可以看到通过让不同的Q分布在broker上实现均衡负载，可以让某些broker来负担多一点的Q，也可以平衡的分布Q。不必让消息绑定在Broker上，而是让消息于Q相绑定，用更小的颗粒度去控制消息。同时Q不保存消息的具体内容，而是能通过Q去追溯在Broker的位置。这一招厉害呀，扩展性瞬间提高了一大截。</p>
<h2 id="那个的问题"><a href="#那个的问题" class="headerlink" title="那个的问题"></a>那个的问题</h2><p>说回那个同学的问题，他应该是直接使用了上千的Topic，但是对于MQ来说，不同于Kafka，MQ能够支持上万的Topic(详情参见官方MQ于kafka的比较）。那么MQ是如何管理所拥有的Topic呢？下一次来分析下Broker的源码。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/%E6%8A%80%E6%9C%AF/" rel="tag"># 技术</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2017/05/19/2017/weex%E5%B0%8F%E5%AE%9E%E8%B7%B5/" rel="prev" title="Weex小实践">
      <i class="fa fa-chevron-left"></i> Weex小实践
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2017/09/17/2017/%E7%BA%BF%E7%A8%8B%E6%B1%A0%E8%B0%83%E4%BC%98/" rel="next" title="线程池调优">
      线程池调优 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%95%B4%E4%BD%93%E7%BB%93%E6%9E%84"><span class="nav-number">1.</span> <span class="nav-text">整体结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Procuder%E5%A6%82%E4%BD%95%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF"><span class="nav-number">2.</span> <span class="nav-text">Procuder如何发送消息</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%82%A3%E4%B8%AA%E7%9A%84%E9%97%AE%E9%A2%98"><span class="nav-number">3.</span> <span class="nav-text">那个的问题</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">RunningEgg</p>
  <div class="site-description" itemprop="description">写下我的点点滴滴，写下我对这个世界的看法，还有我内心锁崇拜的技术</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">89</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">24</span>
        <span class="site-state-item-name">tags</span>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="mailto:guodh_1@163.com" title="E-Mail → mailto:guodh_1@163.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">RunningEgg</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> v5.3.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">Theme – <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.7.1
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  















  

  

</body>
</html>
